package com.ticorp.framework.web.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.filter.OncePerRequestFilter;

import com.ticorp.salmofood.web.bean.BeanUsuario;

public class AuthenticationFilter extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException {

	String accion = request.getParameter("accion");

	if (accion.equals("irCambiarPassword")) {
	    request.getRequestDispatcher("cambiarPassword.htm").forward(request, response);
	} else if (accion.equals("irOlvidePassword")) {
	    request.getRequestDispatcher("olvidePassword.htm").forward(request, response);
	} else if (accion.equals("irLogin")) {
	    request.getRequestDispatcher("login.htm?accion=irLogin").forward(request, response);
	} else if (accion.equals("login")) {
	    request.getRequestDispatcher("login.htm?accion=login").forward(request, response);
	} else if (accion.equals("logout")) {
	    request.getRequestDispatcher("login.htm?accion=logout").forward(request, response);
	} else if (accion.equals("buscarLotesFacturados")) {
	    request.getRequestDispatcher("busquedasLotes.htm?accion=buscarLotesFacturados").forward(request, response);
	} else if (accion.equals("buscarLotesNoFacturados")) {
	    HttpSession session = request.getSession(true);
	    BeanUsuario beanUsuario = (BeanUsuario) session.getAttribute("usuario");
	    String tipoUsuario = beanUsuario.getTipoUsuario();
	    if (tipoUsuario == "interno") {
		request.getRequestDispatcher("busquedasLotes.htm?accion=buscarLotesNoFacturados").forward(request, response);
	    } else {
		request.getRequestDispatcher("login.htm?accion=prohibido").forward(request, response);
	    }
	} else if (accion.equals("buscarDetalleContrato")) {
	    request.getRequestDispatcher("busquedasLotes.htm?accion=buscarDetalleContrato").forward(request, response);
	} else if (accion.equals("irTracingProductoTerminado")) {
	    request.getRequestDispatcher("tracingProductoTerminado.htm?accion=irTracingProductoTerminado").forward(request, response);
	} else if (accion.equals("irTracingProductoTerminado2")) {
	    request.getRequestDispatcher("tracingProductoTerminado2.htm?accion=irTracingProductoTerminado2").forward(request, response);
	} else if (accion.equals("irMateriaPrima")) {
	    request.getRequestDispatcher("materiaPrima.htm?accion=irMateriaPrima").forward(request, response);
	}
    }
}